package com.campus.app.service.impl;

import com.campus.commons.entity.AnnunciateType;
import com.campus.app.mapper.AnnunciateTypeMapper;
import com.campus.app.service.AnnunciateTypesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import com.campus.config.lifecycle.CustomInit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * <p>
 * 公告类型表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2021-01-25
 */
@Service
@Validated
public class AnnunciateTypeServiceImpl extends ServiceImpl<AnnunciateTypeMapper, AnnunciateType> implements AnnunciateTypesService {
    @Autowired
    private AnnunciateTypeMapper annunciateTypeMapper;

    @Override
    public Result<List<AnnunciateType>> getAllTypes() {
        return Result.result(annunciateTypeMapper.selectList(null));
    }

    @Override
    public Result addType(AnnunciateType annunciateType) {
        boolean res = save(annunciateType);
        saveOrUpdate(res,annunciateType);
        return Result.result(res);
    }

    @Override
    public Result delType(Integer id) {
        boolean res = removeById(id);
        //更新缓存
        if (res) {
            AnnunciateType.annunciateTypeMap.remove(id);
        }
        return Result.result(res);
    }

    @Override
    public Result upType(AnnunciateType annunciateType) {
        boolean res = updateById(annunciateType);
        saveOrUpdate(res,annunciateType);
        return Result.result(res);
    }

    private void saveOrUpdate(boolean res,AnnunciateType annunciateType) {
        if (res) {
            AnnunciateType.annunciateTypeMap.put(annunciateType.getId(), annunciateType);
        }
    }

}
